// pages/mine/agency/index.js
const service = require('../../../service/port.js')
const rules = require('../../../utils/util.js')
import Toast from '../../../miniprogram_npm/@vant/weapp/toast/toast.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    show: false,
    radio: '',
    levelId: '',
    chooseLevel: '',
    audit: false,
    agentLevelList: [],
    name: '',
    phone: '',
    errorText: '',
    errorText1: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    service.getAgentStatus(wx.getStorageSync('userInfo').id, res => {
      if (res.data.appStu=='0'){
        this.setData({
          audit:true
        })
      }else{
        this.setData({
          audit: false
        })
      }
      console.log(res)
    })

  },

  choose: function() {
    service.getDicLevel(res => {
      console.log(res)
      this.setData({
        agentLevelList: res.data
      })
      this.setData({
        show: true
      });
    })

  },
  onClose: function() {
    this.setData({
      show: false
    });
  },
  onChange(event) {
    this.setData({
      radio: event.detail,
    });
  },
  onChangephone(e) {
    console.log(e.detail)
    if (!rules.regPhone(e.detail)) {
      this.setData({
        errorText1: '手机号格式有误！',
      });
    } else {
      this.setData({
        errorText1: '',
      });
    }
  },
  blurEvent1(e) {
    if (e.detail.value == '') {
      this.setData({
        errorText1: '手机号不能为空！',
      });
    } else {
      this.setData({
        errorText1: '',
        phone: e.detail.value
      });
    }
  },
  blurEvent(e) {

    if (e.detail.value == '') {
      this.setData({
        errorText: '姓名不能为空！',
      });
    } else {
      this.setData({
        errorText: '',
        name: e.detail.value
      });
    }
  },
  onClick(event) {
    console.log(event.currentTarget);
    const {
      name
    } = event.currentTarget.dataset;
    const item = event.currentTarget.dataset.item
    this.setData({
      radio: name,
      levelId: item.id,
      chooseLevel: item.name
    });
  },
  confirm: function() {

    if (this.data.name == '') {
      this.setData({
        errorText: '姓名不能为空！',
      });

      return
    };
    if (this.data.phone == '') {
      this.setData({
        errorText1: '手机号不能为空！',
      });
      return
    };
    if (!rules.regPhone(this.data.phone)) {
      this.setData({
        errorText1: '手机号格式有误！',
      });
      return
    }
    if (this.data.levelId == '') {
      Toast.fail('请选择等级');
      return
    }
    service.applyAgent({
      "customerId": wx.getStorageSync('userInfo').id,
      "name": this.data.name,
      "phone": this.data.phone,
      "levelId": this.data.levelId
    }, res => {
      Toast.success('申请成功');
      this.setData({
        audit: true
      })
    })

  }
})